package cn.doitedu.datayiee_service17.dao;

import cn.doitedu.datayiee_service17.pojo.SessionAgrBean;
import cn.doitedu.datayiee_service17.util.GetHbaseConnection;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScanner;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.stereotype.Repository;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

@Repository
public class SessionAgrDaoImpl implements SessionAgrDao {

    @Override
    public List<HashMap<String, String>> getSessions(Long ts,String start_row,int limit) throws Exception {
        Connection conn = GetHbaseConnection.getConnection();
        Table tb = conn.getTable(TableName.valueOf("session_agr"));

        Scan scan = new Scan();
        scan.withStartRow(Bytes.toBytes(start_row));
        scan.setLimit(limit);

        ResultScanner resultScanner = tb.getScanner(scan);
        Iterator<Result> iter = resultScanner.iterator();

        ArrayList<HashMap<String, String>> beanLst = new ArrayList<>();

        // 迭代每一行结果
        while(iter.hasNext()){
            Result result = iter.next();

            HashMap<String, String> columnsMap = new HashMap<>();

            // 迭代一行中的每一列
            CellScanner cellScanner = result.cellScanner();
            while(cellScanner.advance()){
                Cell cell = cellScanner.current();
                byte[] valueBytes = CellUtil.cloneValue(cell);
                byte[] qualifierBytes = CellUtil.cloneQualifier(cell);
                columnsMap.put(new String(qualifierBytes),new String(valueBytes));
            }

            beanLst.add(columnsMap);
        }


        return beanLst;
    }
}
